﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Zzva.DomainObjectReg;
using Zzva.Common;
using Zzva.DomainObject;
using System.Collections.ObjectModel;
using Zzva.Report;
using Zzva.ReportExcel;
using System.Collections; 

namespace Zzva.UIWin
{
    public partial class TFrmRegDirSoftType : TFrmRegDirBase
    {

        TRegDirSoftType mvarRegDirSoftType;



        protected override void FilterByValue()
        {
            try
            {
                TFrmFilterByValue FrmFilterByValue;
                
                ////FrmFilterByValue = new TFrmFilterByValue((TRegDirBase)mvarRegDirSoftType, GetListAliasField());
                FrmFilterByValue = new TFrmFilterByValue((TRegDirBase)mvarRegDirSoftType, GetListAliasField(), GetCurColumnObozn());                
                
                
                FrmFilterByValue.EventReady += new EventHandler(FrmFilterByValue_Ready);
                FrmFilterByValue.ShowDialog();
            }

            catch (Exception e1) { throw e1; }
            finally { }
        }











































































































        protected override void Report()
        {
            try
            {

                TReportBase lReportBase;

                //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                //Здесь достаемимя класса Отчета из установок
                //и по технологии Позднего связывания 
                //создаем объект Отчет
                //А пока так

                lReportBase = new TReportExcel();

                lReportBase.Title = mvarRegDirSoftType.TitleFull;

                object lList = mvarRegDirSoftType.GetCurListView();

                //lReportBase.CreateReport(lList);
                //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                //Здесь достаем парметр из установок:
                //Как мы хтим видеть поля отчета, представляющие объекты:
                //развернутыми по всем полям, или только строковое представление
                //А пока так по  умолчанию
                lReportBase.CreateReport(lList, EReportFieldView.ExpandAsObject);

            }
            catch (Exception e1) { MessageBox.Show(e1.Message); }
            finally { }
        }


        protected override void ViewCurrentList()
        {
            try
            {
                object hhh = mvarRegDirSoftType.GetCurListView();

                bool lFlgStateLoadForm;
                lFlgStateLoadForm = mvarFlgStateLoadForm;
                if (mvarFlgStateLoadForm == false) { mvarFlgStateLoadForm = true; }

                dgvMain.DataSource = null;
                dgvMain.Rows.Clear();
                dgvMain.DataSource = hhh;

                DataGridViewColumn lDataGridViewColumn = dgvMain.Columns["Id"];
                lDataGridViewColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
                lDataGridViewColumn.Resizable = DataGridViewTriState.False;
                lDataGridViewColumn.Width = 0;
                mvarFlgStateLoadForm = lFlgStateLoadForm;
                TDirSoftType lCurItem;
                lCurItem = (TDirSoftType)mvarRegDirSoftType.GetCurItem();
                int lCurIndex;

                if (lCurItem != null)
                {
                    lCurIndex = GetIndexForId(dgvMain.Rows, lCurItem.Id);
                    if (lCurIndex == -1) { throw (new CommonException("Текущая запись не найдена")); }
                    dgvMain.CurrentCell = dgvMain.Rows[lCurIndex].Cells[mvarFlgColumnIndexCur];
                }

                this.Text = mvarRegDirSoftType.TitleFull;

            }
            catch (Exception e) { throw e; }
            finally { }
        }


        #region(constructor)
        public TFrmRegDirSoftType(TRegDirSoftType pRegDirSoftType)
            : base()
        {
            InitializeComponent();
            mvarRegDirSoftType = pRegDirSoftType;
            ViewCurrentList();

            if (mvarRegDirSoftType.CheckEventUpdateListIsNull() == true)
            {mvarRegDirSoftType.EventUpdateList += new EventHandler(RegDirBase_UpdateList);}

            if (mvarRegDirSoftType.CheckEventFindDirIsNull() == true)
            {mvarRegDirSoftType.EventFindDir += new EventHandler(RegDirBase_FindDir);}
        }
        #endregion






        protected override void SetCurItem(int pCurItem)
        {
            try { mvarRegDirSoftType.SetCurItem(pCurItem); }
            catch (Exception e1) { throw e1; }
            finally { }
        }


        protected override void FrmDirBase_Ready(object Sender, EventArgs ArgsBase)
        {
            try
            {
                int lId;
                TDirSoftTypeNew lDir;
                TDirEventArgs lDirEventArgs;

                lDirEventArgs = (TDirEventArgs)ArgsBase;
                lId = lDirEventArgs.Id;
                lDir = (TDirSoftTypeNew)lDirEventArgs.Dir;

                if (lId == 0) { AddDir(lDir); }
                else { EditDir(lDir); }
            }
            catch (Exception e) { throw e; }
            finally { }
        }



        #region Find...

        //Установить курсор на заданном объекте
        public override void FindByDir(TDirBase pDir)
        {
            try{mvarRegDirSoftType.FindByDir(pDir);}
            catch (Exception e1) { MessageBox.Show(e1.Message); }
            finally { }
        }

        protected override void FindPrev(TFilterValue pFindValue)
        {
            try{mvarRegDirSoftType.FindPrev(pFindValue);}
            catch (Exception e1) { throw e1; }
            finally { }
        }

        protected override void FindNext(TFilterValue pFindValue)
        {
            try{mvarRegDirSoftType.FindNext(pFindValue);}
            catch (Exception e1) { throw e1; }
            finally { }
        }

        protected override void Find(TFilterValue pFindValue)
        {
            try{mvarRegDirSoftType.Find(pFindValue);}
            catch (Exception e1) { throw e1; }
            finally { }
        }

        #endregion


        #region Filter...

        protected override void FilterAdd(TFilterValue pFilterValue)
        {
            try{mvarRegDirSoftType.FilterAdd(pFilterValue);}
            catch (Exception e1) { throw e1; }
            finally { }
        }

        protected override void FilterDel()
        {
            try{mvarRegDirSoftType.FilterDel();}
            catch (Exception e1) { throw e1; }
            finally { }
        }

        #endregion


        protected override void Sort(TSortValue pSortValue)
        {
            try{mvarRegDirSoftType.Sort(pSortValue);}
            catch (Exception e1) { throw e1; }
            finally { }
        }


        protected override void Delete()
        {
            try{mvarRegDirSoftType.Delete();}
            catch (Exception e1) { throw e1; }
            finally { }
        }


        #region(Edit)

        private void EditDir(TDirSoftTypeNew pDir)
        {
            try{mvarRegDirSoftType.Edit(pDir);}
            catch (Exception e1) { throw e1; }
            finally { }
        }

        protected override void Edit()
        {
            try
            {
                TFrmDirSoftType FrmDirSoftType;
                TDirSoftType lCurDir = (TDirSoftType)mvarRegDirSoftType.GetCurItem();
                FrmDirSoftType = new TFrmDirSoftType(lCurDir);
                FrmDirSoftType.EventReady += new EventHandler(FrmDirBase_Ready);
                FrmDirSoftType.ShowDialog(); 
            }
            catch (Exception e1) { throw e1; }
            finally { }
        }

        #endregion


        protected void AddDir(TDirSoftTypeNew pDir)
        {
            try{mvarRegDirSoftType.Add(pDir);}
            catch (Exception e1) { throw e1; }
            finally { }
        }


        protected override void Ready()
        {
            try
            {
                TDirSoftType lDirSoftType = (TDirSoftType)mvarRegDirSoftType.GetCurItem();
                TDirEventArgs lDirEventArgs;
                lDirEventArgs = new TDirEventArgs(lDirSoftType, lDirSoftType.Id);
                FireEventReady(lDirEventArgs);
                this.Close();
            }

            catch (Exception e) { MessageBox.Show(e.Message); }
            finally { }
        }


        #region(Add)

        protected override void Add()
        {
            try
            {
                TFrmDirSoftType FrmDirSoftType;
                FrmDirSoftType = new TFrmDirSoftType();
                FrmDirSoftType.EventReady += new EventHandler(FrmDirBase_Ready);
                FrmDirSoftType.ShowDialog(); 
            }
            catch (Exception e1) { throw e1; }
            finally { }
        }

        #endregion



       //// public TFrmRegDirSoftType()
       //// {
       ////     InitializeComponent();
       //// }

    }
}
